package com.example.libo.huihui.activity;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.libo.huihui.R;
import com.example.libo.huihui.adapter.FragmentAdapter;
import com.example.libo.huihui.databinding.ActivityMainBinding;
import com.example.libo.huihui.fragment.GuoFragment;
import com.example.libo.huihui.fragment.HaitaoFragment;
import com.example.libo.huihui.fragment.QingdanFragment;
import com.example.libo.huihui.fragment.ShaiwuFragment;
import com.example.libo.huihui.fragment.ZixunFragment;

public class MainActivity extends FragmentActivity{
    private FragmentAdapter adapter;
    private Fragment[] findFgtArr;
    private String[] titles = {"晒物","资讯","海淘","国内","清单"};
    private boolean isOpen = false;
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(MainActivity.this, R.layout.activity_main);

        init();
        initDrawer();
    }

    private void initDrawer() {
        binding.navigationView.setItemIconTintList(null);
        ColorStateList csl = getResources().getColorStateList(R.color.color_white);
        binding.navigationView.setItemTextColor(csl);

        binding.ibToggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isOpen){  //打开状态
                    binding.drawerlayout.closeDrawer(Gravity.LEFT);
                }else{  //未打开状态
                    binding.drawerlayout.openDrawer(Gravity.LEFT);
                }
                isOpen = !isOpen;
            }
        });

        binding.drawerlayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
            }
            @Override
            public void onDrawerOpened(View drawerView) {
                isOpen = true;
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                isOpen = false;
            }
            @Override
            public void onDrawerStateChanged(int newState) {
            }
        });

        binding.navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Toast.makeText(MainActivity.this,"点击了菜单" + item.getTitle(),Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    private void init() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        binding.findPager.setOffscreenPageLimit(4);
        findFgtArr = new Fragment[]{new ShaiwuFragment(), new ZixunFragment(), new HaitaoFragment(), new GuoFragment(), new QingdanFragment()};

        setTitle();

        binding.etSearchbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,SearchActivity.class));
            }
        });
    }

    private void setTitle(){
        adapter = new FragmentAdapter(getSupportFragmentManager(), titles, findFgtArr);
        binding.findPager.setAdapter(adapter);
        binding.tab.setupWithViewPager(binding.findPager);
        binding.tab.getTabAt(3).select();
    }

}
